package com.dcrzl.gazgmh.module.mh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.dcrzl.gazgmh.common.entity.mh.MhMenu;
import com.dcrzl.gazgmh.common.mapper.mh.MhMenuMapper;
import com.dcrzl.gazgmh.common.service.impl.BaseServiceImpl;
import com.dcrzl.gazgmh.common.utils.PageInfo;
import com.dcrzl.gazgmh.common.utils.Result;
import com.dcrzl.gazgmh.common.utils.SysConstant;
import com.dcrzl.gazgmh.module.mh.service.MhMenuService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.*;

@Service("mhMenuService")
public class MhMenuServiceImpl extends BaseServiceImpl<MhMenuMapper, MhMenu> implements MhMenuService {

    private LambdaQueryWrapper<MhMenu> commonWrapper(MhMenu mhMenu) {
        LambdaQueryWrapper<MhMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Objects.nonNull(mhMenu.getId()), MhMenu::getId, mhMenu.getId());
        wrapper.eq(Objects.nonNull(mhMenu.getParentId()), MhMenu::getParentId, mhMenu.getParentId());
        wrapper.eq(Objects.nonNull(mhMenu.getStatus()), MhMenu::getStatus, mhMenu.getStatus());
        wrapper.like(StringUtils.hasText(mhMenu.getName()), MhMenu::getName, mhMenu.getName());
        wrapper.in(!CollectionUtils.isEmpty(mhMenu.getIdList()), MhMenu::getId, mhMenu.getIdList());
        wrapper.eq(Objects.nonNull(mhMenu.getShowStatus()), MhMenu::getShowStatus, mhMenu.getShowStatus());
        return wrapper;
    }

    @Override
    public Result<MhMenu> selectOne(MhMenu mhMenu) {
        LambdaQueryWrapper<MhMenu> wrapper = this.commonWrapper(mhMenu);
        return super.selectOne(wrapper);
    }

    @Override
    public Result<List<MhMenu>> selectList(MhMenu mhMenu) {
        LambdaQueryWrapper<MhMenu> wrapper = this.commonWrapper(mhMenu);
        wrapper.orderByAsc(MhMenu::getOrderNum).orderByDesc(MhMenu::getCreateTime);
        return super.selectList(wrapper);
    }

    @Override
    public PageInfo<MhMenu> selectPage(MhMenu mhMenu) {
        LambdaQueryWrapper<MhMenu> wrapper = this.commonWrapper(mhMenu);
        wrapper.orderByAsc(MhMenu::getOrderNum).orderByDesc(MhMenu::getCreateTime);
        return super.selectPage(mhMenu, wrapper);
    }

    @Override
    public PageInfo<MhMenu> selectUPage(MhMenu mhMenu) {
        PageInfo<MhMenu> mhMenuPageInfo = this.selectPage(mhMenu);
        List<MhMenu> list = mhMenuPageInfo.getList();
        Set<Long> menuIdSet = new HashSet<>();
        list.forEach(mhMenu1 -> {
            if(Objects.nonNull(mhMenu1.getParentId())&&mhMenu1.getParentId()!=0){
                menuIdSet.add(mhMenu1.getParentId());
            }
        });
        if(!CollectionUtils.isEmpty(menuIdSet)){
            Result<List<MhMenu>> listResult = this.selectList(new MhMenu().setIdList(menuIdSet));
            if(listResult.isSuccess()){
                List<MhMenu> mhMenuList = listResult.getData();
                Map<Long,MhMenu> map = new HashMap<>();
                mhMenuList.forEach(mhMenu1 -> {
                    map.put(mhMenu1.getId(), mhMenu1);
                });
                list.forEach(mhMenu1 -> {
                    if(Objects.nonNull(mhMenu1.getParentId())&&mhMenu1.getParentId()!=0){
                        mhMenu1.setParentMhMenu(map.get(mhMenu1.getParentId()));
                    }
                });
            }
        }
        return mhMenuPageInfo;
    }

    @Override
    public Result<Integer> removeById(Serializable id) {
        MhMenu mhMenu = new MhMenu().setStatus(SysConstant.DATA_DELETE_STATUS)
                .setId((Long) id);
        return this.updateById(mhMenu);
    }

}